define([
  'text!' + ELMP.resource('components/selectPerson/index.html', 'lib'),
  ELMP.resource('mixins/emitter.js', 'lib')
],
  /**
   * @returns {typeof import('types/vue/types/index').default}
   */
  function (template, emitter) {
    let list = []
    return {
      template,
      name: 'SelectPerson',
      model: {
        prop: 'value',
        event: 'input'
      },
      mixins: [emitter],
      props: {
        roleCode: {
          type: String,
          default: ''
        },
        oid: {
          type: String,
          default: ''
        },
        orgId: {
          type: String,
          default: ''
        },
        process: {
          type: Function
        },
        value: {
          type: [String, Array]
        },
        showAll: {
          type: Boolean,
          default: false
        },
        disabled: {
          type: Boolean,
          default: false
        },
        multiple: {
          type: Number,
          default: 1
        }
      },
      watch: {
        value() {
          this.renderMember()
        },
        disabled() {
          this.renderMember()
        },
      },
      mounted() {
        this.renderMember()
      },
      methods: {
        // 渲染选人组件
        async renderMember () {
          let arr = []
          if (this.value) {
            if (typeof this.value === 'string') {
              const res = await $.elApi.org.getUsers({ userIds: this.value })
              this.value = res.records
            }
            if (Array.isArray(this.value)) {
              arr = this.value
            } else {
              arr = [this.value]
            }
          }
          let url = ELMP.url('/org/v1/users/fuzzy')
          if (this.orgId) {
            url = ELMP.url('/org/v1/peoples')
          }
          const that = this
          $(this.$el).selectMember({
            members: arr,
            key: 'id',
            showAll: this.showAll,
            disabled: this.disabled,
            multiple: this.multiple,
            panel: [
              {
                title: $.i18n.get('people_info'), // '人员信息'
                url,
                // 接口参数
                data: (params, type) => {
                  return {
                    fuzzyQueryCondition: params.term,
                    orgId: that.orgId,
                    containerOid: that.oid,
                    roleCode: that.roleCode
                  }
                },
                search: true,
                // 对查询出的数据的处理
                process: (res) => {
                  let result = []
                  if (this.process) {
                    result = $.Utils.unique(list.concat(this.process(res)), 'code')
                  } else {
                    if (Array.isArray(res.res.data)) {
                      result = res.res.data
                    } else {
                      result = res.res.data.records
                    }
                    list = $.Utils.unique(
                      list.concat(result),
                      'code'
                    )
                  }
                  return result
                }
              }
            ],
            onComplete: (ids, members = []) => {
              let result = members.map((item) => {
                const person = list.find((person) => person.code === item.code)
                return person
              })
              if (this.multiple === 1) {
                result = result[0]
              }
              this.$emit('input', result)
              this.$emit('change', result)
              this.dispatch('ElFormItem', 'el.form.change', [result])
            }
          })
        }
      }
    }
  })
